package cz.acies.uniql.sql.element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.dom4j.Element;
import org.dom4j.dom.DOMElement;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.sql.UniQLExpr;

public class UniQLConditionGroup extends UniQLCriterion
{
	private static final long serialVersionUID = -7833108074947754369L;

	/**
	 * 
	 */
	public UniQLConditionGroup() {
	}

	/**
	 * @param dialect
	 */
	public UniQLConditionGroup(Dialect dialect) {
		super(dialect);
	}

	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#translate()
	 */
	public String translate() throws Exception {	
		StringBuffer sb = new StringBuffer();
		if (getConditions() != null && !getConditions().isEmpty()) {
			sb.append("(");
			sb.append(super.translate().trim());
			sb.append(") ");
		}
		return sb.toString();
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.element.UniQLCriterion#toString()
	 */
	public String toString() {
		return "UniQLConditionGroup( "+super.toString()+" )";
	}
	
	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML()
	 */
	public String transToXML() throws Exception {
		DOMElement node = new DOMElement("UniQLConditionGroup");
		transToXML(node);
		return node.asXML();
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML(org.dom4j.dom.DOMElement)
	 */
	public DOMElement transToXML(DOMElement node) throws Exception {
		node.setName("UniQLConditionGroup");
		if (this.getConditions() != null) {
			for (Iterator<UniQLCriterionElement> i = this.getConditions().iterator(); i.hasNext();) {
				UniQLCriterionElement element = i.next();
				Object obj = element.getCondition();
				if(obj instanceof UniQLCondition) {
					UniQLCondition expr = (UniQLCondition)obj;
					int op = element.getOperator();
					if (expr instanceof UniQLExpr) {
						DOMElement param = expr.transToXML(new DOMElement("UniQLCriterion"));
						if (param != null) {
							param.setAttribute("operator", (op == 0) ? "AND" : "OR");
							node.appendChild(param);	
						}
					}
				} else if(obj instanceof UniQLConditionGroup){
					UniQLConditionGroup expr = (UniQLConditionGroup)obj;
					int op = element.getOperator();
					if (expr instanceof UniQLExpr) {
						DOMElement param = expr.transToXML(new DOMElement("UniQLCriterion"));
						if (param != null) {
							param.setAttribute("operator", (op == 0) ? "AND" : "OR");
							node.appendChild(param);	
						}
					}
				}
			}
		}		
		return node;
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(java.lang.String)
	 */
	public UniQLExpr xmlToExpr(String xml) throws Exception {
		System.out.println("UniQLConditionGroup.xmlToExpr(xml)");
		return null;
	}

	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(org.dom4j.Element)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public UniQLExpr xmlToExpr(Element node) throws Exception {
		ArrayList params = new ArrayList(node.elements());
		for (int i = 0; i < params.size(); i++) {
			Element param = (Element)params.get(i);
			String nodename = param.getName();
			UniQLExpr expr = null; 
			if (nodename.trim().equals("UniQLCondition")) {
				expr = new UniQLCondition(getDialect()).xmlToExpr(param);
				if (expr != null) {
					String op = param.attributeValue("operator");	 
					this.addCondition((UniQLCondition)expr, op);
				}
			}
		}
		return this;
	}
	
}
